﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.user
{
    /// <summary>
    /// 用户权限设置
    /// </summary>
    public partial class corpuserauthset : UserPage
    {
        protected int uid = SASRequest.GetInt("uid", 0);
        protected string opmode = SASRequest.GetString("opmode");
        protected UserInfo uinfo = new UserInfo();

        protected void editauth_Click(object sender, EventArgs e)
        {
            string auths = "";
            int allcount = 0;
            bool createAdmin = false;

            if (opmode == "setcorpmanager") createAdmin = true;

            DataTable maindt = new DataTable();
            DataTable subdt = new DataTable();
            maindt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.MainMenu);
            subdt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.SubMenu);

            foreach (System.Data.DataRow dr in maindt.Rows)
            {
                int subcount = 0;
                foreach (System.Data.DataRow srow in subdt.Select("parentid=" + dr["menuid"]))
                {
                    string authstr = "";
                    authstr += SASRequest.GetString("auth_main_" + allcount) + ",";
                    if (authstr.Trim(',') == "")
                    {
                        allcount++;
                        continue;
                    }

                    authstr = authstr + SASRequest.GetString("auth_" + allcount);
                    authstr = authstr.Trim(',').Replace(',', '|');

                    if (authstr != "") authstr += "|" + srow["frameid"] + ",";
                    allcount++;
                    auths += authstr;
                }
            }

            if (!Users.SetCorpManagerAuth(SASRequest.GetInt("ajaxcorpdatalist_value", 0), SASRequest.GetInt("ajaxuserdata_value", 0), createAdmin, auths))
            {
                base.RegisterStartupScript("PAGETEMPLATE", "权限设置出错，请与管理员联系！");
                return;
            }

            base.RegisterStartupScript("PAGE", "parent.document.getElementById('reclick').click();parent.Lightview.hide();");
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            if (opmode == "setmanager")
            {
                uinfo = Users.GetUser(uid);
            }

            editauth.Click += new EventHandler(editauth_Click);
        }

        #endregion
    }
}